package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BlockModel;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.view.choice.adapter.ChoiceViewPagerAdapter;
import com.taptech.doufu.ui.view.choice.listener.OnLayoutHeight;
import com.taptech.doufu.ui.view.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ChoiceTopicOtherPagerView extends LinearLayout {
    private Context context;
    private TabLayout tlTop;
    private TTHomeViewPager mViewPager;
    private List mSweepListViews;
    private EventBean eventData;


    public ChoiceTopicOtherPagerView(Context context) {
        super(context);
    }

    public ChoiceTopicOtherPagerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicOtherPagerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicOtherPagerView(final Context context,
                                     List<BlockModel.Item> list,
                                     List<NovelModel> defaultNovelList,
                                     int index,
                                     final EventBean eventData) {
        this(context);
        this.context = context;
        this.eventData = eventData;
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_complex_pager_view, null);
        tlTop = view.findViewById(R.id.tlTop);
        tlTop.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewPager = view.findViewById(R.id.mViewPager);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_TL_TAB,eventData);
                ((ChoiceTopicOtherContentView)mSweepListViews.get(position)).loadData();
                if (heightCache.containsKey(position)) {
                    setLayoutHeight(heightCache.get(position));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initViews(list,defaultNovelList,index == 0?false:true);
        addView(view);
    }


    private HashMap<Integer,Integer> heightCache = new HashMap<>();

    public void setLayoutHeight(int height) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,height+ScreenUtil.dip2px(context, 66/*底部按钮高度*/));
        mViewPager.setLayoutParams(params);
    }



    private void initViews(List<BlockModel.Item> list, List<NovelModel> defaultNovelList,boolean needInit) {
        mSweepListViews = new ArrayList<>();
        List titles = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            BlockModel.Item model = list.get(i);
            titles.add(model.getTitle());
            List<NovelModel> initList;
            if (i == 0 && !needInit) {
                initList = defaultNovelList;
            }else{
                initList = new ArrayList<>();
            }
            final int index = i;
            ChoiceTopicOtherContentView view = new ChoiceTopicOtherContentView(context,initList, model, eventData, new OnLayoutHeight() {
                @Override
                public void onResult(int height) {
                    heightCache.put(index,height);
                    setLayoutHeight(height);
                }
            });
            mSweepListViews.add(view);
        }
        ChoiceViewPagerAdapter viewPagerAdapter = new ChoiceViewPagerAdapter(mSweepListViews,titles);
        mViewPager.setAdapter(viewPagerAdapter);
        tlTop.setupWithViewPager(mViewPager);


    }
}
